package vmcs.store;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import vmcs.common.VMCSException;

public abstract class FileImpl implements DataImpl {

	private Properties prop;
	private String fileName;

	public FileImpl(String file) {
		fileName = file;
	}

	public int getNumOfItems() {
		String nm = prop.getProperty("NumOfItems");
		int nmi;

		nmi = Integer.parseInt(nm);
		return nmi;

	}

	public void setNumOfItems(int vl) {
		prop.setProperty("NumOfItems", String.valueOf(vl));
	}

	public void initialize() throws VMCSException {
		try {
			prop = new Properties(System.getProperties());
			FileInputStream stream = new FileInputStream(fileName);
			prop.load(stream);
			stream.close();
		} catch (Exception e) {
			throw new VMCSException("PropertyLoader.initialize",
					"Error loading STORE properties: " + e);
		}
	}

	public String getValue(String key) {
		return prop.getProperty(key);
	}

	public void setValue(String key, String value) {
		prop.setProperty(key, value);
	}

	public void saveProperty() throws VMCSException {
		try {

			FileOutputStream os = new FileOutputStream(fileName);
			prop.store(os, "-----------fileName----------");
			os.close();
		} catch (IOException e) {
			throw new VMCSException("PropertyLoader.save",
					"Error in saving the property file" + e);
		}
	}

}
